package com.utils;

import java.io.IOException;
/*
 * Copyright 2017, Tencent Inc
 * All rights reserved.
 * 
 * Created on 2017年9月12日
 */
import java.util.Base64;
import java.util.Random;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import com.alibaba.fastjson.JSONObject;

public class FaceSign {

    /**
     * 生成 Authorization 签名字段
     * 
     * @param appId   1251060754
     * @param secretId   AKIDYuB2g3aB1buBJXnN0NULxvgBUXKrj6i5
     * @param secretKey  B6qKXXGjGbkpeJhpa6LJ8i5AzYsxlSs4
     * @param bucketName
     * @param expired
     * @return
     * @throws Exception
     */
    public static String appSign(long appId, String secretId, String secretKey, String bucketName,
            long expired) throws Exception {
        long now = SystemClock.now() / 1000;
        int rdm = Math.abs(new Random().nextInt());
        String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d", appId, bucketName,
                secretId, now, now + expired, rdm);
        byte[] hmacDigest = HmacSha1(plainText, secretKey);
        byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
        System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
        System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length,
                plainText.getBytes().length);
        return Base64Encode(signContent);
    }

    /**
     * 生成 base64 编码
     * 
     * @param binaryData
     * @return
     */
    public static String Base64Encode(byte[] binaryData) {
        String encodedstr = Base64.getEncoder().encodeToString(binaryData);
        return encodedstr;
    }

    /**
     * 生成 hmacsha1 签名
     * 
     * @param binaryData
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(secretKey);
        byte[] HmacSha1Digest = mac.doFinal(binaryData);
        return HmacSha1Digest;
    }

    /**
     * 生成 hmacsha1 签名
     * 
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(String plainText, String key) throws Exception {
        return HmacSha1(plainText.getBytes(), key);
    }

    
    public static void main(String[] args) throws Exception {
    	
    	HttpClient client = new HttpClient();
    	client.getParams().setContentCharset("UTF-8");
    	PostMethod postMethod = new PostMethod("http://recognition.image.myqcloud.com/face/detect");
    	
    	postMethod.setRequestHeader("host",   	     "recognition.image.myqcloud.com");
    	postMethod.setRequestHeader("content-type",  "application/json");
    	postMethod.setRequestHeader("authorization",  appSign(1251060754,"AKIDYuB2g3aB1buBJXnN0NULxvgBUXKrj6i5","B6qKXXGjGbkpeJhpa6LJ8i5AzYsxlSs4","50du",SystemClock.now() + 1*24*3600000L));
    
    	postMethod.addParameter("appid", "1251060754");
    	postMethod.addParameter("url", "http://qiniu.static.50dufei.top/155390655991563942693572.jpg");
    	
    	try {
			int status = client.executeMethod(postMethod);
			if (status == HttpStatus.SC_OK) {
				String result = postMethod.getResponseBodyAsString();
				System.out.println(result);
			} else {
				System.out.println(status);
			}
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
